﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HyperPin_Database_Editor
{
    public partial class HPKeyboardSettings : HPSettingsPanel
    {
        static string iniKeyDelay = "Key_Delay";
        static string iniStart = "Start";
        static string iniExit = "Exit";
        static string iniUp = "Up";
        static string iniDown = "Down";
        static string iniSkipUp = "SkipUp";
        static string iniSkipDown = "SkipDown";
        static string iniFlyer = "Flyer";
        static string iniInstruction = "Instruction";
        static string iniGenre = "Genre";
        static string iniService = "Service";

        public static string[] buttons =
        {
            "Joy0", // button code 0
            "Joy1", // button code 1
            "Joy2", // button code 2
            "Joy3", // button code 3
            "Joy4", // button code 4
            "Joy5", // button code 5
            "Joy6", // button code 6
            "Joy7", // button code 7
            "Joy8", // button code 8
            "Joy9", // button code 9
            "Joy10", // button code 10
            "Joy11", // button code 11
            "Joy12", // button code 12
            "Joy13", // button code 13
            "Joy14", // button code 14
            "Joy15", // button code 15
            "Joy16", // button code 16
            "Joy17", // button code 17
            "Joy18", // button code 18
            "Joy19", // button code 19
            "Joy20", // button code 20
            "Joy21", // button code 21
            "Joy22", // button code 22
            "Joy23", // button code 23
            "Joy24", // button code 24
            "Joy25", // button code 25
            "Joy26", // button code 26
            "Joy27", // button code 27
            "Joy28", // button code 28
            "Joy29", // button code 29
            "Joy30", // button code 30
            "Joy31", // button code 31
        };

        public static string[] buttonNames()
        {
            List<string> buttonNamesToReturn = new List<string>();
            foreach (string buttonName in buttons)
            {
                if (buttonName != "")
                {
                    buttonNamesToReturn.Add(buttonName);
                }
            }

            return buttonNamesToReturn.ToArray();
        }

        public static string[] keys =
        {
		    "", // key code = 0
            "", // key code = 1
            "", // key code = 2
            "", // key code = 3
            "", // key code = 4
            "", // key code = 5
            "", // key code = 6
            "", // key code = 7
            "Backspace", // key code = 8
            "Tab", // key code = 9
            "", // key code = 10
            "", // key code = 11
            "", // key code = 12
            "Enter", // key code = 13
            "", // key code = 14
            "", // key code = 15
            "", // key code = 16
            "Control", // key code = 17
            "", // key code = 18
            "Pause/Break", // key code = 19
            "Caps Lock", // key code = 20
            "", // key code = 21
            "", // key code = 22
            "", // key code = 23
            "", // key code = 24
            "", // key code = 25
            "", // key code = 26
            "Esc", // key code = 27
            "", // key code = 28
            "", // key code = 29
            "", // key code = 30
            "", // key code = 31
            "Spacebar", // key code = 32
            "Page Up", // key code = 33
            "Page Down", // key code = 34
            "End", // key code = 35
            "Home", // key code = 36
            "Left Arrow", // key code = 37
            "Up Arrow", // key code = 38
            "Right Arrow", // key code = 39
            "Down Arrow", // key code = 40
            "", // key code = 41
            "", // key code = 42
            "", // key code = 43
            "", // key code = 44
            "Insert", // key code = 45
            "Delete", // key code = 46
            "", // key code = 47
            "0", // key code = 48
            "1", // key code = 49
            "2", // key code = 50
            "3", // key code = 51
            "4", // key code = 52
            "5", // key code = 53
            "6", // key code = 54
            "7", // key code = 55
            "8", // key code = 56
            "9", // key code = 57
            "", // key code = 58
            "", // key code = 59
            "", // key code = 60
            "", // key code = 61
            "", // key code = 62
            "", // key code = 63
            "", // key code = 64
            "A", // key code = 65
            "B", // key code = 66
            "C", // key code = 67
            "D", // key code = 68
            "E", // key code = 69
            "F", // key code = 70
            "G", // key code = 71
            "H", // key code = 72
            "I", // key code = 73
            "J", // key code = 74
            "K", // key code = 75
            "L", // key code = 76
            "M", // key code = 77
            "N", // key code = 78
            "O", // key code = 79
            "P", // key code = 80
            "Q", // key code = 81
            "R", // key code = 82
            "S", // key code = 83
            "T", // key code = 84
            "U", // key code = 85
            "V", // key code = 86
            "W", // key code = 87
            "X", // key code = 88
            "Y", // key code = 89
            "Z", // key code = 90
            "", // key code = 91
            "", // key code = 92
            "", // key code = 93
            "", // key code = 94
            "", // key code = 95
            "Numpad 0", // key code = 96
            "Numpad 1", // key code = 97
            "Numpad 2", // key code = 98
            "Numpad 3", // key code = 99
            "Numpad 4", // key code = 100
            "Numpad 5", // key code = 101
            "Numpad 6", // key code = 102
            "Numpad 7", // key code = 103
            "Numpad 8", // key code = 104
            "Numpad 9", // key code = 105
            "Multiply", // key code = 106
            "Add", // key code = 107
            "", // key code = 108
            "Subtract", // key code = 109
            "Decimal", // key code = 110
            "Divide", // key code = 111
            "F1", // key code = 112
            "F2", // key code = 113
            "F3", // key code = 114
            "F4", // key code = 115
            "F5", // key code = 116
            "F6", // key code = 117
            "F7", // key code = 118
            "F8", // key code = 119
            "F9", // key code = 120
            "F10", // key code = 121
            "F11", // key code = 122
            "F12", // key code = 123
            "F13", // key code = 124
            "F14", // key code = 125
            "F15", // key code = 126
            "", // key code = 127
            "", // key code = 128
            "", // key code = 129
            "", // key code = 130
            "", // key code = 131
            "", // key code = 132
            "", // key code = 133
            "", // key code = 134
            "", // key code = 135
            "", // key code = 136
            "", // key code = 137
            "", // key code = 138
            "", // key code = 139
            "", // key code = 140
            "", // key code = 141
            "", // key code = 142
            "", // key code = 143
            "Num Lock", // key code = 144
            "ScrLk", // key code = 145
            "", // key code = 146
            "", // key code = 147
            "", // key code = 148
            "Left Shift", // key code = 149
            "Right Shift", // key code = 150
            "", // key code = 151
            "", // key code = 152
            "", // key code = 153
            "", // key code = 154
            "", // key code = 155
            "", // key code = 156
            "", // key code = 157
            "", // key code = 158
            "", // key code = 159
            "", // key code = 160
            "", // key code = 161
            "", // key code = 162
            "", // key code = 163
            "", // key code = 164
            "", // key code = 165
            "", // key code = 166
            "", // key code = 167
            "", // key code = 168
            "", // key code = 169
            "", // key code = 170
            "", // key code = 171
            "", // key code = 172
            "", // key code = 173
            "", // key code = 174
            "", // key code = 175
            "", // key code = 176
            "", // key code = 177
            "", // key code = 178
            "", // key code = 179
            "", // key code = 180
            "", // key code = 181
            "", // key code = 182
            "", // key code = 183
            "", // key code = 184
            "", // key code = 185
            "; :", // key code = 186
            "= +", // key code = 187
            ",", // key code = 188
            "- _", // key code = 189
            ".", // key code = 190
            "/ ?", // key code = 191
            "` ~", // key code = 192
            "", // key code = 193
            "", // key code = 194
            "", // key code = 195
            "", // key code = 196
            "", // key code = 197
            "", // key code = 198
            "", // key code = 199
            "", // key code = 200
            "", // key code = 201
            "", // key code = 202
            "", // key code = 203
            "", // key code = 204
            "", // key code = 205
            "", // key code = 206
            "", // key code = 207
            "", // key code = 208
            "", // key code = 209
            "", // key code = 210
            "", // key code = 211
            "", // key code = 212
            "", // key code = 213
            "", // key code = 214
            "", // key code = 215
            "", // key code = 216
            "", // key code = 217
            "", // key code = 218
            "[ {", // key code = 219
            "\\ |", // key code = 220
            "] }", // key code = 221
            "\" '", // key code = 222
            "", // key code = 223
            "", // key code = 224
            "", // key code = 225
            "", // key code = 226
            "", // key code = 227
            "", // key code = 228
            "", // key code = 229
            "", // key code = 230
            "", // key code = 231
            "", // key code = 232
            "", // key code = 233
            "", // key code = 234
            "", // key code = 235
            "", // key code = 236
            "", // key code = 237
            "", // key code = 238
            "", // key code = 239
            "", // key code = 240
            "", // key code = 241
            "", // key code = 242
            "", // key code = 243
            "", // key code = 244
            "", // key code = 245
            "", // key code = 246
            "", // key code = 247
            "", // key code = 248
            "", // key code = 249
            "", // key code = 250
            "", // key code = 251
            "", // key code = 252
            "", // key code = 253
            "", // key code = 254
            "" // key code = 255        
        };

        public static string[] keyNames()
        {
            List<string> keyNamesToReturn = new List<string>();
            foreach (string keyName in keys)
            {
                if (keyName != "")
                {
                    keyNamesToReturn.Add(keyName);
                }
            }

            return keyNamesToReturn.ToArray();
        }

        public HPKeyboardSettings()
        {
            InitializeComponent();

            /*
             * This code is (was) intended to allow the user to press the key
             * they want assigned rather than having to select from the list.
             * It will require a little extra work, see below in the combo_KeyDown
             * routine, to make it work properly.
             * Not done for version 1.0
             */
            //comboStart.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboExit.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboUp.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboDown.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboSkipUp.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboSkipDown.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboFlyer.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboInstructions.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboGenre.KeyDown += new KeyEventHandler(combo_KeyDown);
            //comboService.KeyDown += new KeyEventHandler(combo_KeyDown);

            // Populate the combo boxes with all the key names.
            foreach(string keyName in keys)
            {
                if (keyName != "")
                {
                    comboStart.Items.Add(keyName);
                    comboExit.Items.Add(keyName);
                    comboUp.Items.Add(keyName);
                    comboDown.Items.Add(keyName);
                    comboSkipUp.Items.Add(keyName);
                    comboSkipDown.Items.Add(keyName);
                    comboFlyer.Items.Add(keyName);
                    comboInstructions.Items.Add(keyName);
                    comboGenre.Items.Add(keyName);
                    comboService.Items.Add(keyName);
                }
            }
        }

        public override void doLoadSettings()
        {
            checkKeyDelay.Checked = readSettingBool(iniKeyDelay, false);

            comboStart.Text = keys[readSettingInt(iniStart, 13)];
            comboExit.Text = keys[readSettingInt(iniExit, 27)];
            comboUp.Text = keys[readSettingInt(iniUp, 150)];
            comboDown.Text = keys[readSettingInt(iniDown, 149)];
            comboSkipUp.Text = keys[readSettingInt(iniSkipUp, 191)];
            comboSkipDown.Text = keys[readSettingInt(iniSkipDown, 90)];
            comboFlyer.Text = keys[readSettingInt(iniFlyer, 70)];
            comboInstructions.Text = keys[readSettingInt(iniInstruction, 73)];
            comboGenre.Text = keys[readSettingInt(iniGenre, 71)];
            comboService.Text = keys[readSettingInt(iniService, 9)];
        }

        public override void doSaveSettings()
        {
            writeSettingBool(iniKeyDelay, checkKeyDelay.Checked);

            writeSettingInt(iniStart, findKeyCode(comboStart.Text));
            writeSettingInt(iniExit, findKeyCode(comboExit.Text));
            writeSettingInt(iniUp, findKeyCode(comboUp.Text));
            writeSettingInt(iniDown, findKeyCode(comboDown.Text));
            writeSettingInt(iniSkipUp, findKeyCode(comboSkipUp.Text));
            writeSettingInt(iniSkipDown, findKeyCode(comboSkipDown.Text));
            writeSettingInt(iniFlyer, findKeyCode(comboFlyer.Text));
            writeSettingInt(iniInstruction, findKeyCode(comboInstructions.Text));
            writeSettingInt(iniGenre, findKeyCode(comboGenre.Text));
            writeSettingInt(iniService, findKeyCode(comboService.Text));
        }

        private int findKeyCode(string keyName)
        {
            // Wow, this is a horrible way to reverse the keycodes.
            for (int keyCode = 0; keyCode < keys.Length; keyCode++)
            {
                if (keyName == keys[keyCode])
                {
                    return keyCode;
                }
            }

            // key not found returns 0;
            return 0;
        }

        /*
        private void combo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // http://stackoverflow.com/questions/2569268/net-difference-between-right-shift-and-left-shift-keys
            //Console.WriteLine(String.Format("{0}: {1}", (int)e.KeyCode, e.KeyData));
 
         * [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern short GetAsyncKeyState(Keys key);
            
           if (e.KeyCode == Keys.ShiftKey) {
                if (GetAsyncKeyState(Keys.LShiftKey) < 0) Console.WriteLine("Left");
                if (GetAsyncKeyState(Keys.RShiftKey) < 0) Console.WriteLine("Right");
           }
        }
        */
    }
}
